/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import net.sourceforge.stripes.util.Base64;

/**
 *
 * @author fikri
 */
public class Util {

    public int getAge(String DOB, String today, String df) {
        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        int age = 0;
        int factor = 0;
        try {
            Date date1 = new SimpleDateFormat(df).parse(DOB);
            Date date2 = new SimpleDateFormat(df).parse(today);
            cal1.setTime(date1);
            cal2.setTime(date2);
//            if (cal2.get(Calendar.DAY_OF_YEAR) < cal1.get(Calendar.DAY_OF_YEAR)) {
//                factor = -1;
//            }
            age = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + factor;
        } catch (Exception ex) {
        }

        return age;
    }
    
    public String encrypt(String plainText) {
        MessageDigest md;

        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            String msg = "SHA not found, encryption cannot continue, no recovery possible";
            throw new RuntimeException(msg, e);
        }

        try {
            md.update(plainText.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            String msg = "UTF-8 encoding not found, no recovery possible";
            throw new RuntimeException(msg, e);
        }

        byte[] raw = md.digest();

        //String pwd = new BASE64Encoder().encode(raw);
        String pwd = Base64.encodeBytes(raw);
        return pwd.length() > 50 ? pwd.substring(0, 50) : pwd;
    }
    
    
    public static void main (String[] args ) {
        DecimalFormat formatter = new DecimalFormat("#,###,###.##");
        BigDecimal amaun = new BigDecimal("076059914").multiply(new BigDecimal("0.01"));
        System.out.println("amount = " + amaun);
        System.out.println("amount = " + formatter.format(amaun));
        BigDecimal baki = new BigDecimal("760599.14"); 
        
    }
}
